package com.muqi.app.qlearn.student.ui;

import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.qlearn.modles.PayOrderId;
import com.muqi.app.qlearn.modles.PayTypeInfo;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

public class MoneyChargeActivity extends BaseFragmentActivity implements OnClickListener {
	public static final int RESULT_CODE_WORDS = 2001;
	private TextView mChargeMoneyK;
	private EditText mChargeTime;
	private TextView mTv_type;
	private TextView mTv_danwei;
	private Button mNext;
	private PayTypeInfo payTypeInfo;
	private int type; /*0 按月结算  1 按年结算*/
	private int month;
	private PayOrderId mPayOrderId;
	private String studentId;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_money_charge);
	}

	@Override
	protected void onInit() {
		
		findViewById(R.id.charge_outer).setOnTouchListener(new OnTouchListener() {
			
			@SuppressLint("ClickableViewAccessibility") @Override
			public boolean onTouch(View v, MotionEvent event) {
				dismissSoftKeyboard();
				return false;
			}
		});
		
		mChargeTime = (EditText) findViewById(R.id.charge_time);
		mChargeMoneyK=(TextView) findViewById(R.id.charge_money_k);
		mTv_danwei=(TextView) findViewById(R.id.tv_danwei);
		mTv_type=(TextView) findViewById(R.id.tv_type);
	
		mNext = (Button) findViewById(R.id.next_charge);
		
		mNext.setOnClickListener(this);
		
		mChargeTime.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0) {
					mChargeMoneyK.setText(getTotalMoney());
				}else{
					mChargeMoneyK.setText(0+"");
				}

			}
		});
		

	}

	/**
	 * 根据填写的月数，计算总共的天数
	 * 
	 * @return
	 */
	protected String getTotalMoney() {
		String money = "";
		if (payTypeInfo != null) {
			if (type == 1) {
				month = Integer.parseInt(mChargeTime.getText().toString()) * 12;
				money = Float.parseFloat(payTypeInfo.price) * month + "";

			} else if (type == 0) {
				month = Integer.parseInt(mChargeTime.getText().toString());
				money = Float.parseFloat(payTypeInfo.price) * month
				+ "";
			}
		}
		return money;
	}



	@Override
	protected void onData() {
		Intent intent = getIntent();
		if (intent != null) {
			studentId=intent.getStringExtra("student_id");
			Log.e("===", studentId);
			Log.e("===", mSpUtil.getCurrentChildId());
			payTypeInfo = (PayTypeInfo) intent.getSerializableExtra("typeInfo");
		    if("year".equals(payTypeInfo.period)){
		    	type=1;
		    	mTv_danwei.setText("年");
		    	mTv_type.setText("年数：");
		    }else if("month".equals(payTypeInfo.period)){
		    	type=0;
		    	mTv_danwei.setText("月");
		    	mTv_type.setText("月数：");
		    }
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.next_charge:
			createOrder();
			break;
		default:
			break;
		}
	}
	
	private void createOrder() {
		if(!TextUtils.isEmpty(mChargeTime.getText().toString())){
			showLoading();
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("token", mSpUtil.getToken());
			params.put("value", month);
			params.put("student_id", studentId);
			params.put("vip_type_id", payTypeInfo.id);
			
			MyAsyncHttp.get(this, params, NetWorkApi.CREATE_CHARGE_ORDER_ID, new AsyncCallBackListener() {
				
				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode, String request,String result) {
					hideLoading();
					Log.e("===", result);
					if (resuleCode == RESULTCODE.SUCCESS) {
						try {
							JSONObject json = new JSONObject(result.toString());
							JSONObject payOrderId = json.getJSONObject("data");
							mPayOrderId = PayOrderId.fromJson(payOrderId);
							if(mPayOrderId!=null){
								Intent i = new Intent(MoneyChargeActivity.this,MoneyPayActivity.class);
								i.putExtra("item", mPayOrderId);
								startActivity(i);
								MoneyChargeActivity.this.finish();
							}else{
								showErrorWithState();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				}
			});
		}else{
			showToast("请填写时间");
		}
	}

	public void onBack(View v){
		this.finish();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}
	
}
